package com.gp.dwt.controller;

import com.gp.dwt.base.JavaMailTool;
import com.gp.dwt.base.Result;
import com.gp.dwt.base.ResultCode;
import com.gp.dwt.base.ValidateCode;
import com.gp.dwt.dto.UserLoginDto;
import com.gp.dwt.dto.UserRegisterDto;
import com.gp.dwt.service.UserService;
import com.gp.dwt.utils.DtoNullUtil;
import com.gp.dwt.utils.EmailValidateUtil;
import com.gp.dwt.utils.SmsCodeGenerationUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import javax.servlet.http.HttpSession;

/**
 * @author 小杜
 */
@RestController
@RequestMapping("/user")
@Api(value = "用户的登录注册接口",tags = "用户的登录注册接口")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private JavaMailTool javaMailTool;

    @Autowired
    private SmsCodeGenerationUtil smsCodeGenerationUtil;
    @ApiOperation("用户注册获取验证码接口，需要先填写用户名等")
    @PostMapping("/feign/userGetValidation")
    public Result userRegister(HttpSession session, @RequestBody UserRegisterDto userRegisterDto){
        if (DtoNullUtil.stringEmpty(userRegisterDto.getUserName())){
            return Result.responseResult(ResultCode.USER_NAME_EMPTY);
        }
        if (DtoNullUtil.stringEmpty(userRegisterDto.getPassword())){
            return Result.responseResult(ResultCode.PASSWORD_EMPTY);
        }
        if (DtoNullUtil.stringEmpty(userRegisterDto.getEmail())){
            return Result.responseResult(ResultCode.EMAIL_EMPTY);
        }
        if (!EmailValidateUtil.checkEmail(userRegisterDto.getEmail())){
            return Result.responseResult(ResultCode.EMAIL_IS_ILLEGAL);
        }
        //进行业务逻辑判断
        return userService.userRegister(session,userRegisterDto);
    }

    @ApiOperation("用户注册时，验证验证码，验证通过则注册")
    @PostMapping("/feign/userValidateRegister")
    public Result userValidateRegister(HttpSession session, @RequestBody UserRegisterDto userRegisterDto){
        System.out.println("验证时的sessionId:"+session.getId());
        if (DtoNullUtil.stringEmpty(userRegisterDto.getCode())){
            return Result.responseResult(ResultCode.CODE_IS_EMPTY);
        }
        //进行业务逻辑判断
        return userService.userValidateRegister(session,userRegisterDto);
    }

    @ApiOperation("用户通过用户名密码登录")
    @PostMapping("/feign/userLoginByUserName")
    public Result userLoginByUserName( @RequestBody UserLoginDto userLoginDto){
        if (DtoNullUtil.stringEmpty(userLoginDto.getUserName())){
            return Result.responseResult(ResultCode.USER_NAME_EMPTY);
        }
        if (DtoNullUtil.stringEmpty(userLoginDto.getPassword())){
            return Result.responseResult(ResultCode.PASSWORD_EMPTY);
        }
        return userService.userLoginByUserName(userLoginDto);
    }

    @ApiOperation("用户邮箱登录时获取验证码")
    @PostMapping("/feign/userLoginByPhoneGetValidation")
    public Result userLoginByPhoneGetValidation(HttpSession session, @RequestBody UserLoginDto userLoginDto){
        if (EmailValidateUtil.checkEmail(userLoginDto.getEmail())){
            //生成验证码
            ValidateCode smsCode=smsCodeGenerationUtil.generate();
            session.setAttribute(userLoginDto.getEmail(),smsCode);
            System.out.println(smsCode);
            javaMailTool.sendEmail(userLoginDto.getEmail(),smsCode.getCode());
            return Result.responseResult();
        }
        return Result.responseResult(ResultCode.EMAIL_IS_ILLEGAL);
//        return systemClient.sendValidationCode(userLoginDto.getPhone());
    }

    @ApiOperation("用户判别了验证码，使用邮箱登录,未注册自动注册登录")
    @PostMapping("/feign/userLoginByPhone")
    public Result userLoginByPhone(HttpSession session, @RequestBody UserLoginDto userLoginDto){
        ValidateCode smsCode=(ValidateCode) session.getAttribute(userLoginDto.getEmail());
        if (smsCode==null){
            return Result.responseResult(ResultCode.NO_VALIDATION_SENT);
        }
        if (smsCode.isExpired()){
            return Result.responseResult(ResultCode.VALIDATION_EXPIRED);
        }
        if (!userLoginDto.getCode().equals(smsCode.getCode())){
            return Result.responseResult(ResultCode.VALIDATION_WRONG);
        }
        //验证码正确，删除验证码信息。
        session.removeAttribute(userLoginDto.getEmail());
        return userService.userLoginByEmail(userLoginDto);
    }
}
